package com.example;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(path = "/update")
public class UpdateController {
    @Autowired
    private UserRepository userRepository;

    @PatchMapping(path = "/one")
    public @ResponseBody String one(@RequestParam String name) {
        // 根据编号获得被Optional包装的记录对象
        Optional<User> optional = userRepository.findById(1);
        if (optional.isPresent()) {
            // 获得解包后的记录对象
            User user = optional.get();
            // 更新记录
            user.setName(name);
            // 保存记录
            userRepository.save(user);
            // 响应结果
            return "Updated!";
        }
        return "Not found!";
    }
}
